define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Banklist, $window, $timeout, GoDialog, Auth, GoPage,$rootScope) {
		//画面加载
		$scope.input = new Object();
		$scope.datas = new Object();
		$scope.count =null;
		
		$scope.$on('$ionicView.beforeEnter', function() {
			var Infos = Auth.get_loginInfo();
			$scope.issue_type = Infos.issue_type;//1买家    2卖家
		}, false);
		
		$scope.$on('$ionicView.afterEnter', function(){
			//动态设置高度
			 GoPage.init_top($scope, 44,60);
			
		},false);
		
		
		$scope.init_data = function() {
			$scope.firstHide = false;//避免闪现“没有数据”
			$scope.input.nPage = 1;
			Banklist.bank_list($scope.input.nPage, function(data) {
				console.log(data)
				if(data != null){
					$scope.datas = data;
				}
				$scope.firstHide = true;
			})
		}
		$scope.init_data();
		
		//设置“默认使用”
		$scope.set_default = function(id) {
			Banklist.bank_default(id, function(data) {
				if(data.success == true) {
					GoDialog.toast('设置成功');
					$scope.init_data();
				} else{
					GoDialog.alert("提示",data.msg);
				}
			})
		}

		//删除
		$scope.delete_button = function(index) {
			GoDialog.confirm("删除","确定要删除吗？",function(){
				Banklist.bank_delete(index, function(data) {
					if(data.success == true) {
						GoDialog.toast('删除成功');
						$scope.init_data();
					} else{
						GoDialog.alert("提示",data.msg);
					}
				})
			})
		}
		
		/****************上下拉刷新****************/
		$scope.isLoad = false; //可以加载
		$scope.doRefresh = function() {
			//防止重复加载
			$scope.isLoad = true;
			Banklist.bank_list(1, function(data) {
				if(data != null) {
					$scope.datas = data;
					$scope.input.nPage = 1;
				} else
					$scope.datas = new Object();
				$scope.input.nPage = 1;
			},
			function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			});
		};
		//加载更多
		$scope.doRefreshMore = function() {
			//防止重复加载
			$scope.isLoad = true;
			if($scope.datas.bank == undefined) {
				$scope.input.nPage = 0;
			} else
			if($scope.datas.page.count <= $scope.datas.bank.length) {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				if($scope.datas.page.count<$scope.input.nPage*10){
					$scope.input.nPage -=1;
					$scope.count = $scope.input.nPage *10;
				}else{
					$scope.count = null;
				}
			}
			Banklist.bank_list($scope.input.nPage + 1, function(data) {
				if(data != null) {
					if($scope.datas.bank != undefined) {
						if($scope.count!=null)
						$scope.datas.bank.splice($scope.count,$scope.datas.page.count-$scope.count);
						$scope.datas.bank = $scope.datas.bank.concat(data.bank);
						$scope.input.nPage += 1;
					} else {
						$scope.datas = data;
						$scope.input.nPage = 1;
					}
				}
			},
			function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			});
		};
		/****************上下拉刷新****************/

		//编辑页面
		$scope.onshow_add = function(id) {
			GoPage.go_states("t-address-add-yh", {
				id: id
			});
		};
		
		//注册了全局广播
		$rootScope.$on('editBankEvent', function(event, data) {
			$scope.init_data();
		});

	}

	controller.$inject = ['$scope', 'Banklist', '$window', '$timeout', 'GoDialog', 'Auth', 'GoPage','$rootScope'];

	return app.registerController("banklistctrl",controller);
});